# ------------------------------------------------------------------------------------------------
### Merging election and LGBTQ-free zones data
# ------------------------------------------------------------------------------------------------

# Load required datasets
load("../data/raw/parl_elections_2001-2019.Rda")
load("../data/raw/lgbt_free_zones.Rda")
load("../data/raw/unadopted_zones.Rda")

# ------------------------------------------------------------------------------------------------
### Create LGBTQ_election_data (treatment includes only resolutions issued pre-2019-election):
# ------------------------------------------------------------------------------------------------
# Keep only the LGBTQ-free zones existing prior to the 2019 election (wave 1)
lgbt_free_zones_w1 <- subset(lgbt_free_zones, date < "2019-10-13")

# Create treatment indicators for different resolutions and levels
LGBTQ_election_data <- parl_elections %>%
  # Any resolution type, treatment = being within the adopting territorial unit  
  mutate(province_any_type = case_when(
    (id_voivodship %in% lgbt_free_zones_w1$id) ~ 1, 
    TRUE ~ 0),
    county_any_type = case_when(
      (id_county %in% lgbt_free_zones_w1$id) ~ 1, 
      TRUE ~ 0),
    municipality_any_type = case_when(
      (id %in% lgbt_free_zones_w1$id) ~ 1, 
      TRUE ~ 0), 
    any_level_any_type = case_when(
      (province_any_type == 1) | 
      (county_any_type == 1) | 
      (municipality_any_type == 1) ~ 1, 
      TRUE ~ 0),
    # The number of times the municipality has been treated at different levels
    sum_count_any_type = municipality_any_type + county_any_type +
      province_any_type,
    # Same for only type 1 resolutions (against the "LGBT ideology")
      province_type_1 = map_int(id_voivodship, function(id) {
        ifelse(any(lgbt_free_zones_w1$id == id & lgbt_free_zones_w1$type == 1), 1, 0)
        }),
      county_type_1 = map_int(id_county, function(id) {
        ifelse(any(lgbt_free_zones_w1$id == id & lgbt_free_zones_w1$type == 1), 1, 0)
        }),
      municipality_type_1 = map_int(id, function(id) {
        ifelse(any(lgbt_free_zones_w1$id == id & lgbt_free_zones_w1$type == 1), 1, 0)
        }),
      any_level_type_1 = case_when(
        (province_type_1 == 1) |
        (county_type_1 == 1) |
        (municipality_type_1 == 1) ~ 1,
        TRUE ~ 0),
    # Same for only type 2 resolutions (Family Charter)
      province_type_2 = map_int(id_voivodship, function(id) {
        ifelse(any(lgbt_free_zones_w1$id == id & lgbt_free_zones_w1$type == 2), 1, 0)
      }),
      county_type_2 = map_int(id_county, function(id) {
        ifelse(any(lgbt_free_zones_w1$id == id & lgbt_free_zones_w1$type == 2), 1, 0)
      }),
      municipality_type_2 = map_int(id, function(id) {
        ifelse(any(lgbt_free_zones_w1$id == id & lgbt_free_zones_w1$type == 2), 1, 0)
      }),
      any_level_type_2 = case_when(
        (province_type_2 == 1) |
        (county_type_2 == 1) |
        (municipality_type_2 == 1) ~ 1,
        TRUE ~ 0),
    # The number of resolution types affecting the municipality  
    sum_count_any_level = any_level_type_1 + any_level_type_2)    

# Remove all unneeded variables
LGBTQ_election_data <- LGBTQ_election_data %>%
  select(-province_type_1, -county_type_1, -municipality_type_1, -province_type_2, -county_type_2, -municipality_type_2)

# Create a treatment indicator and election year variable
LGBTQ_election_data$treat = ifelse(LGBTQ_election_data$edate > "2019-03-26" & LGBTQ_election_data$any_level_any_type==1, 1, 0)
LGBTQ_election_data$eyear = year(LGBTQ_election_data$edate)

# Create indicators for resolutions proposed but not adopted before the 2019 elections
# (includes discussed or dismissed or rejected resolutions)
unadopted_zones <- unadopted_zones %>%
  filter(date <= "2019-10-13")

LGBTQ_election_data <- LGBTQ_election_data %>%
  mutate(any_level_proposed = if_else(
      id_voivodship %in% unadopted_zones$id |
        id_county %in% unadopted_zones$id |
        id %in% unadopted_zones$id, 1, 0))

# Correct the class of variables:
LGBTQ_election_data$any_level_any_type <- as.factor(LGBTQ_election_data$any_level_any_type)
LGBTQ_election_data$turnout <- as.numeric(LGBTQ_election_data$turnout)
LGBTQ_election_data$municipality_any_type <- as.factor(LGBTQ_election_data$municipality_any_type)

# ------------------------------------------------------------------------------------------------
### Create LGBTQ_election_data_w1w2 (treatment includes all resolutions, i.e., pre- and post-2019-election):
# ------------------------------------------------------------------------------------------------
# Create treatment indicators for different resolutions and levels
LGBTQ_election_data_w1w2 <- parl_elections %>%
  # Any resolution type, treatment = being within the adopting territorial unit  
  mutate(province_any_type = case_when(
    (id_voivodship %in% lgbt_free_zones$id) ~ 1, 
    TRUE ~ 0),
    county_any_type = case_when(
      (id_county %in% lgbt_free_zones$id) ~ 1, 
      TRUE ~ 0),
    municipality_any_type = case_when(
      (id %in% lgbt_free_zones$id) ~ 1, 
      TRUE ~ 0), 
    any_level_any_type = case_when(
      (province_any_type == 1) | 
        (county_any_type == 1) | 
        (municipality_any_type == 1) ~ 1, 
      TRUE ~ 0),
    # The number of times the municipality has been treated at different levels 
    sum_count_any_type = municipality_any_type + county_any_type +
      province_any_type,
    # Same for only type 1 resolutions (against the "LGBT ideology")
      province_type_1 = map_int(id_voivodship, function(id) {
        ifelse(any(lgbt_free_zones$id == id & lgbt_free_zones$type == 1), 1, 0)
      }),
      county_type_1 = map_int(id_county, function(id) {
        ifelse(any(lgbt_free_zones$id == id & lgbt_free_zones$type == 1), 1, 0)
      }),
      municipality_type_1 = map_int(id, function(id) {
        ifelse(any(lgbt_free_zones$id == id & lgbt_free_zones$type == 1), 1, 0)
      }),
      any_level_type_1 = case_when(
        (province_type_1 == 1) |
          (county_type_1 == 1) |
          (municipality_type_1 == 1) ~ 1,
        TRUE ~ 0),
    # Same for only type 2 resolutions (Family Charter)
      province_type_2 = map_int(id_voivodship, function(id) {
        ifelse(any(lgbt_free_zones$id == id & lgbt_free_zones$type == 2), 1, 0)
      }),
      county_type_2 = map_int(id_county, function(id) {
        ifelse(any(lgbt_free_zones$id == id & lgbt_free_zones$type == 2), 1, 0)
      }),
      municipality_type_2 = map_int(id, function(id) {
        ifelse(any(lgbt_free_zones$id == id & lgbt_free_zones$type == 2), 1, 0)
      }),
      any_level_type_2 = case_when(
        (province_type_2 == 1) |
          (county_type_2 == 1) |
          (municipality_type_2 == 1) ~ 1,
        TRUE ~ 0),
    # The number of resolution types affecting the municipality
    sum_count_any_level = any_level_type_1 + any_level_type_2)

# remove all unneeded variables
LGBTQ_election_data_w1w2 <- LGBTQ_election_data_w1w2 %>%
  select(-province_type_1, -county_type_1, -municipality_type_1, -province_type_2, -county_type_2, -municipality_type_2)

# If double treated with multiple resolutions, keep only earliest date
# If double treated on the same day, keep only one observation
lgbt_free_zones_dates <- lgbt_free_zones %>%
  select(id, date) %>%
  group_by(id) %>%
  filter(date == min(date)) %>%
  ungroup() %>%
  unique()

# Prepare setup for treatment including all resolutions, i.e., pre- and post-2019-election:
LGBTQ_election_data_w1w2 <- LGBTQ_election_data_w1w2 %>%
  left_join(lgbt_free_zones_dates %>% select(id,date), by = c("id_voivodship"="id"), relationship = "many-to-one") %>%
  mutate(province_date = date) %>%
  select(-date)

LGBTQ_election_data_w1w2 <- LGBTQ_election_data_w1w2 %>%
  left_join(lgbt_free_zones_dates %>% select(id,date), by = c("id_county"="id"), relationship = "many-to-one") %>%
  mutate(county_date = date) %>%
  select(-date) 

LGBTQ_election_data_w1w2 <- LGBTQ_election_data_w1w2 %>%
  left_join(lgbt_free_zones_dates %>% select(id,date), by = c("id"="id"), relationship = "many-to-one") %>%
  mutate(municipality_date = date) %>%
  select(-date)

LGBTQ_election_data_w1w2 <- LGBTQ_election_data_w1w2 %>%
  mutate(date = pmin(province_date,county_date,municipality_date, na.rm = TRUE), 
         wave_id = ifelse(any_level_any_type== 1 & date<="2019-10-13", 1, 
                          ifelse(any_level_any_type== 1 & date>"2019-10-13" & date<="2023-10-15",2,0)),
         eyear = year(edate),
         ryear = ifelse(is.na(date), 0, year(date))) 

        
LGBTQ_election_data_w1w2$eyear_id <- LGBTQ_election_data_w1w2 %>% 
  group_by(eyear) %>%
  group_indices()

LGBTQ_election_data_w1w2 <- LGBTQ_election_data_w1w2 %>%
  mutate(eyear_indicator = ifelse(wave_id==1, 6, 
                                ifelse(wave_id==2, 7,
                                ifelse(ryear==0, 0, NA)))) %>%
  group_by(id) %>%
  fill(eyear_indicator, .direction = "downup")


# Create a dummy indicating treatment status: 
LGBTQ_election_data_w1w2 <- LGBTQ_election_data_w1w2 %>%
  group_by(id) %>%
  mutate(treat = ifelse(eyear_indicator==eyear_id |  eyear_id >= eyear_indicator & eyear_indicator!=0 , 1, 0))

# remove all unneeded variables
LGBTQ_election_data_w1w2 <- LGBTQ_election_data_w1w2 %>%
  select(-province_date, -county_date, -municipality_date, -date, -ryear, -eyear_indicator, -eyear_id)


# Create indicators for resolutions proposed but not adopted before the 2019 elections
# (includes discussed or dismissed or rejected resolutions)
unadopted_zones <- unadopted_zones %>%
  filter(date <= "2019-10-13")

LGBTQ_election_data_w1w2 <- LGBTQ_election_data_w1w2 %>%
  mutate(any_level_proposed = if_else(
    id_voivodship %in% unadopted_zones$id |
      id_county %in% unadopted_zones$id |
      id %in% unadopted_zones$id, 1, 0))

# Correct the class of variables
LGBTQ_election_data_w1w2$wave_id <- as.character(LGBTQ_election_data_w1w2$wave_id)
LGBTQ_election_data_w1w2$any_level_any_type <- as.factor(LGBTQ_election_data_w1w2$any_level_any_type)
LGBTQ_election_data_w1w2$turnout <- as.numeric(LGBTQ_election_data_w1w2$turnout)
LGBTQ_election_data_w1w2$municipality_any_type <- as.factor(LGBTQ_election_data_w1w2$municipality_any_type)

# Add wave_id indicator to LGBTQ_election_data from LGBTQ_election_data_w1w2 by id and eyear
LGBTQ_election_data <- LGBTQ_election_data %>%
  left_join(LGBTQ_election_data_w1w2 %>% select(id, eyear, wave_id), by = c("id", "eyear"))

# ------------------------------------------------------------------------------------------------
### Save the merged datasets:
# ------------------------------------------------------------------------------------------------
# Save main dataset (treatment includes only resolutions issued pre-2019-election)
save(LGBTQ_election_data, file = "../data/processed/LGBTQ_election_data.Rda")
# Save dataset for table 12 (treatment includes all resolutions, i.e., pre- and post-2019-election)
save(LGBTQ_election_data_w1w2, file = "../data/processed/LGBTQ_election_data_w1w2.Rda")
